﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Net;
using System.Xml.Linq;
using System.ServiceModel.Syndication;

namespace GadgetControl
{
    public partial class GadgetControl: UserControl
    {

        
        public string[] harga = new string[10];
        public string[] title = new string[10];
        public string[] gambar = new string[10];
        public string[] network = new string[10];
        public string[] kamera = new string[10];
        private int selected = 0;
        int i = 0;
        public string tampung1=null;
        public string tampung2=null;
        
        public GadgetControl()
        {
            InitializeComponent();
            
        }

        

        public void getRss()
        {
            
           
             
            try
            {


                
                string tmp = comboBox1.SelectedItem.ToString();
                string tmpHarga = null;
                if (comboBox2.SelectedItem != null && comboBox3.SelectedItem != null)
                {
                    tampung1 = "/" + comboBox2.SelectedItem.ToString();
                    tampung2 = "/" + comboBox3.SelectedItem.ToString();
                }
                XmlReader xmlReader = XmlReader.Create("http://22104937.ap01.aws.af.cm/pbk/gadget/" + tmp + tampung1 + tampung2);
                i = 0;
                while (xmlReader.Read())
                {
                    if (xmlReader.NodeType == XmlNodeType.Element)
                    {
                        
                        if(xmlReader.Name=="title")
                        {
                                title[i] = xmlReader.ReadString();
                                
                        }
                        if(xmlReader.Name=="harga")
                        {
                                harga[i] = xmlReader.ReadString();
                            
                        } 
                        if(xmlReader.Name=="img")
                        {
                                gambar[i] = xmlReader.ReadString();
                            
                        } 
                        if(xmlReader.Name=="network")
                        {
                                network[i] = xmlReader.ReadString();
                           
                        } 
                        if(xmlReader.Name=="kamera")
                        {
                                kamera[i] = xmlReader.ReadString();
                            
                        }
                        if (xmlReader.Name == "gadget")
                        {
                            i++;
                            if (i >= 10) break;
                        }
                       }

                    }

               /* linkLabel1.Text = gambar[0] + "\n";
                for (int j = 1; j < 10; j++) {
                    linkLabel1.Text = linkLabel1.Text + gambar[j] + "\n";
                }*/
                List<string> y = gambar.ToList<string>();
                y.RemoveAll(p => string.IsNullOrEmpty(p));
                gambar = y.ToArray();

                List<string> z = title.ToList<string>();
                z.RemoveAll(p => string.IsNullOrEmpty(p));
                title = z.ToArray();

                List<string> w = harga.ToList<string>();
                w.RemoveAll(p => string.IsNullOrEmpty(p));
                harga = w.ToArray();
                selected = 0;
                showImage(gambar[selected],title[selected],harga[selected]);
                timer1.Enabled = true;
            }
            catch(WebException e)
            {
                MessageBox.Show("error");
            }
            
            
        }
            
        private void button1_Click(object sender, EventArgs e)
        {
            
            getRss();
                
        }

        private void showImage(string path,string pathtitle,string pathharga) 
        {
            
            pictureBox1.ImageLocation = path;
            linkLabel1.Text = pathtitle;
            label1.Text = pathharga;
        }

        private void prevImage()
        {
            if (selected == 0)
            {
                selected = gambar.Length - 1 ;
                showImage(gambar[selected],title[selected], harga[selected]);
            }
            else
            {
                selected = selected - 1;
                showImage(gambar[selected],title[selected], harga[selected]);
            }
        }

        private void nextImage()
        {
            if (selected == gambar.Length - 1)
            {
                selected = 0;
                if (gambar[selected] != null && title[selected] != null && harga[selected] !=null)
                showImage(gambar[selected], title[selected], harga[selected]);
            }
            else 
            {
                selected = selected + 1;
                if (gambar[selected] != null && title[selected] != null && harga[selected] != null)
                showImage(gambar[selected], title[selected], harga[selected]);
            }
        }
           

            private void timer1_Tick_1(object sender, EventArgs e)
            {
                nextImage();
            }

            private void button3_Click(object sender, EventArgs e)
            {
                nextImage();
            }

            private void button2_Click(object sender, EventArgs e)
            {
                prevImage();
            }

            private void button4_Click(object sender, EventArgs e)
            {
                if (timer1.Enabled == true) {
                    timer1.Enabled = false;
                    button4.Text = "On";
                }
                else if (timer1.Enabled == false) {
                    timer1.Enabled = true;
                    button4.Text = "off";
                }
            }
        }

       

           
           
    }


